package com.baidu.yyb.model;

import com.baidu.yyb.MMKV.NoteSp;
import com.baidu.yyb.bean.NoteData;
import com.baidu.yyb.bean.UserData;
import com.baidu.yyb.constant.NoteTag;
import com.baidu.yyb.dataBase.DbHelper;
import com.baidu.yyb.dataBase.Dbo;
import com.lt.utils.String.StringUtils;
import com.lt.utils.ThreadPoolUtils;
import com.lt.utils.ViewUtils.LogUtils;

/**
 * @author LiuTong
 */
public class NoteModelImp implements INoteModel {
    private static final String TAG =NoteModelImp.class.getSimpleName();
    private DbHelper dbHelper ;

    @Override
    public void login(UserData userData, OnLoginCallback onLoginCallback) {
        ThreadPoolUtils.getInstance().runTask(new Runnable() {
            @Override
            public void run() {
                UserData currentUSerData = getUserDataWithDatabase();
                if(userData.equals(currentUSerData)){
                    if(onLoginCallback != null) {
                        onLoginCallback.success(currentUSerData);
                    }
                }
            }
        });
    }

    @Override
    public void register(UserData userData, OnRegisterListen onRegisterListen) {
        ThreadPoolUtils.getInstance().runTask(() -> {
            /****
             * 注册时 判断数据库中是否存在
             */
            if(getUserDataWithDatabase()!=null){
                /***
                 * 编辑数据库数据
                 */
                if( Dbo.getInstance().changeInfoWithId(userData)){
                    if(onRegisterListen != null){
                        onRegisterListen.onRegisterSuccess(userData);
                    }
                }else {
                    if(onRegisterListen!=null){
                        onRegisterListen.OnRegisterFail("数据库插入数据错误");
                    }
                }

            }else {
                if(Dbo.getInstance().insertUSer(userData)){
                    if(onRegisterListen != null){
                        onRegisterListen.onRegisterSuccess(userData);
                    }
                }else {
                    if(onRegisterListen!=null){
                        onRegisterListen.OnRegisterFail("数据库插入数据错误");
                    }
                }
            }
        });



    }

    @Override
    public UserData getUserDataWithDatabase() {
        String userId = NoteSp.getString(NoteTag.USER_NAME);
        if(StringUtils.isEmptyOrNull(userId)){
            return null;
        }

        String password = Dbo.getInstance().getPassword(userId);
        String phone = Dbo.getInstance().getPhone(userId);

        if(!StringUtils.isEmptyOrNull(userId)
                && !StringUtils.isEmptyOrNull(password)
                && !StringUtils.isEmptyOrNull(phone)){
            return new UserData(userId,password,phone);
        }
        return null;
    }

    @Override
    public UserData getUserWithSP() {
        String useName = NoteSp.getString(NoteTag.USER_NAME);
        String password = NoteSp.getString(NoteTag.PASSWORD);
        String phone = NoteSp.getString(NoteTag.PHONE);
        if(!StringUtils.isEmptyOrNull(useName)
                && !StringUtils.isEmptyOrNull(password)
                && !StringUtils.isEmptyOrNull(phone)){
            return new UserData(useName,password,phone);

        }
        return null;
    }

    @Override
    public NoteData getNoteDataWithKeyWord(String keyWord) {
        /****
         * 数据库查找
         */
        return null;
    }

    @Override
    public NoteData getNoteDataWithNoteId(String Id) {
        return null;
    }

    @Override
    public void saveUSerDataWithSp(UserData userData) {
        if(userData != null){
            NoteSp.putString(NoteTag.USER_NAME,userData.getUserId());
            NoteSp.putString(NoteTag.PASSWORD,userData.getPassword());
            NoteSp.putString(NoteTag.PHONE,userData.getPhoneNumber());
        }
    }


    @Override
    public void insertUserData(UserData userData) {
        if(Dbo.getInstance().insertUSer(userData)){
            LogUtils.e(TAG,"插入数据成功");
        }
    }

    @Override
    public void deleteUserData(UserData userData) {

    }

    @Override
    public void editUserData(UserData userData) {

    }

    @Override
    public void findUserData(UserData userData) {

    }


    @Override
    public void addNote(NoteData noteData) {

    }

    @Override
    public void deleteNote(NoteData noteData) {

    }

    @Override
    public void editNote(NoteData noteData) {

    }

    @Override
    public void findNote(String keyString) {

    }

    @Override
    public void uploadNote(NoteData noteData) {

    }

    @Override
    public void downloadNote(NoteData noteData) {

    }
}
